<?php
	require_once("jara_config.php");
	require_once("exceptions.php");
	
	$db_conn = null;
	
	function jara_db_connect() {
		global $jconfig, $db_conn;
		if($db_conn == null) {
			@$conn = new mysqli($jconfig->mysql_host, $jconfig->mysql_username, $jconfig->mysql_password, $jconfig->mysql_db);
			if(mysqli_connect_errno()) {
				throw new JaraDatabaseException(1, mysqli_connect_errno(), mysqli_connect_error());
			}
			$db_conn = $conn;
			return $conn;
		}
		else {
			return $db_conn;
		}
	}
	
	function jara_db_query($query) {
		$conn = jara_db_connect();
		$result = $conn->query($query);
		if($conn->errno) {
			throw new JaraDatabaseException(2, $conn->errno, $conn->error, $query);
		}
		if(substr($query, 0, 6) == "select" || substr($query, 0, 4) == "show" || substr($query, 0, 7) == "explain") {
			return $result;
		}
		else {
			return $conn->affected_rows;
		}
	}
?>